import 'package:favorcate/core/model/category_model.dart';
import 'package:favorcate/core/services/json_parse.dart';
import 'package:flutter/material.dart';
import 'package:favorcate/ui/shared/extension/int_extension.dart';

import 'home_category_item.dart';

class ZSHomeContent extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return FutureBuilder<List<ZSCategoryModel>>(
        future: ZSJsonParse.getCategoriesData(),
        builder: (ctx, snapshot) {
          if (!snapshot.hasData)
            return Center(child: CircularProgressIndicator());
          if (snapshot.error != null) return Center(child: Text('请求失败!'));
          final _categories = snapshot.data;
          return GridView.builder(
            padding: EdgeInsets.all(20.px),
            itemCount: _categories?.length,
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisCount: 2,
              crossAxisSpacing: 20.px,
              mainAxisSpacing: 20.px,
              childAspectRatio: 1.5,
            ),
            itemBuilder: (ctx, index) {
              return ZSHomeCategoryItem(_categories![index]);
            },
          );
        });
  }
}

// class ZSHomeContent extends StatefulWidget {
//   @override
//   _ZSHomeContentState createState() => _ZSHomeContentState();
// }
//
// class _ZSHomeContentState extends State<ZSHomeContent> {
//   List<ZSCategoryModel> _categories = [];
//
//   @override
//   void initState() {
//     super.initState();
//
//     //加载数据
//     ZSJsonParse.getCategoriesData().then((result) {
//       // print(result);
//       setState(() {
//         _categories = result;
//       });
//     });
//   }
//
//   @override
//   Widget build(BuildContext context) {
//     return GridView.builder(
//       padding: EdgeInsets.all(20.px),
//       itemCount: _categories.length,
//       gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
//         crossAxisCount: 2,
//         crossAxisSpacing: 20.px,
//         mainAxisSpacing: 20.px,
//         childAspectRatio: 1.5,
//       ),
//       itemBuilder: (ctx, index) {
//         return ZSHomeCategoryItem(_categories[index]);
//       },
//     );
//   }
// }
